package ahblue.cn.tanzi.ui.circle;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;
import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.data.source.ApiCallback;
import ahblue.cn.tanzi.data.source.RemoteRepository;
import ahblue.cn.tanzi.model.circle.CircleCategory;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.ui.home.search.SearchActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import q.rorbin.verticaltablayout.VerticalTabLayout;

/**
 * 显示所有的圈子
 */
public class CircleShowAllActivity extends BaseActivity {

    @BindView(R.id.tablayout_circle_tag)
    VerticalTabLayout mVerticalTabLayout;
    @BindView(R.id.viewPagerCircle)
    ViewPager mViewPagerCircle;

    // 圈子标签
    private List<CircleCategory> mCircleCategoryList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circle_show_all);

        ButterKnife.bind(this);

        initData();
        initView();
    }

    private void initData() {
        RemoteRepository repo = RemoteRepository.getsInstance();
        repo.getBigShotCircleCategory(mCircleCategoryCallback);
    }

    private void initView() {

    }

    @OnClick(R.id.btn_back)
    void onClickBack() {
        finish();
    }

    @OnClick(R.id.layout_search)
    void onClickSearch() {
        final Intent intent = new Intent(this, SearchActivity.class);
        startActivity(intent);
    }

    /**
     * 获取圈子标签的回调接口
     */
    ApiCallback<List<CircleCategory>> mCircleCategoryCallback = new ApiCallback<List<CircleCategory>>() {

        @Override
        public void onSuccess(List<CircleCategory> data) {
//            mCircleCategoryList = data;
            mCircleCategoryList = new ArrayList<>();
            mCircleCategoryList.add(new CircleCategory(-1, "全部"));
            // 初始化tablayout和viewpager
            CircleListPagerAdapter adapter = new CircleListPagerAdapter(getSupportFragmentManager());
            mViewPagerCircle.setAdapter(adapter);
            mVerticalTabLayout.setupWithViewPager(mViewPagerCircle);
        }

        @Override
        public void onFailed(String errorMsg) {

        }
    };

    /**
     * 文章分类ViewPager的adapter。
     */
    class CircleListPagerAdapter extends FragmentPagerAdapter {

        CircleListPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            CircleCategory category = mCircleCategoryList.get(i);
            return CircleShowAllListFragment.newInstance(category.id);
        }

        @Override
        public int getCount() {
            return mCircleCategoryList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return mCircleCategoryList.get(position).category_title;
        }
    }
}
